package com.sh.data.engine.web.openapi.controller.integration;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.*;
import com.sh.data.engine.application.model.request.integration.OfflineSyncRequest;
import com.sh.data.engine.application.model.request.integration.SaveHttpTaskRequest;
import com.sh.data.engine.application.service.integration.ApiHttpApplicationService;
import com.sh.data.engine.application.service.integration.OfflineSyncApplicationService;
import com.sh.data.engine.common.annotation.fuzzyQuery.FuzzyMethod;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;

@Validated
@RestController
@RequestMapping("/openapi/integration")
@Tag(name="数据集成", description="数据集成相关接口")
public class DataIntegrationController {

    @Autowired
    private OfflineSyncApplicationService offlineSyncApplicationService;

    @Autowired
    private ApiHttpApplicationService apiHttpApplicationService;

    // 离线同步任务相关接口
    @Operation(summary = "创建离线同步任务", description = "创建离线同步任务")
    @PostMapping(value = "/createOfflineSyncTask")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_OFFLINE)
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.离线同步任务)
    public RestResponseEntity<Boolean> createOfflineSyncTask(
            @Valid @RequestBody OfflineSyncRequest request) {
        var result = offlineSyncApplicationService.saveOfflineSyncTask(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "保存/编辑任务", description = "保存/编辑任务")
    @PostMapping(value = "/updateOfflineSyncTask")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_OFFLINE)
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.离线同步任务)
    public RestResponseEntity<Boolean> updateOfflineSyncTask(
            @Valid @RequestBody OfflineSyncRequest request) {
        var result = offlineSyncApplicationService.saveOfflineSyncTask(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "删除离线同步任务", description = "删除离线同步任务")
    @GetMapping("/deleteOfflineSyncTask")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_OFFLINE)
    @LogExecution(operation = EngineOperation.DEL, business = Business.离线同步任务)
    public RestResponseEntity<Boolean> deleteOfflineSyncTask(
            @Parameter(description = "离线同步id") @RequestParam(value = "id") Long id) {
        var result = offlineSyncApplicationService.deleteOfflineSyncTask(id);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "查看离线同步任务详情", description = "查看离线同步任务详情")
    @GetMapping("/getOfflineSyncTask")
    public RestResponseEntity<OfflineSyncResponse> getOfflineSyncTask(
            @Parameter(description = "离线同步id", required = true) @RequestParam(value = "id") Long id) {
        var result = offlineSyncApplicationService.getDetail(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listOfflineSyncTasks")
    @Operation(summary = "获取离线同步任务列表", description = "获取离线同步任务列表")
    @FuzzyMethod(fuzzyParamIndex = 2)
    public RestResponseEntity<PagerResponse<OfflineSyncPageResponse>> listOfflineSyncTasks(
            @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
            @RequestParam(required = false, defaultValue = "1")
            Integer pageNum,
            @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
            @RequestParam(required = false, defaultValue = "10")
            Integer pageSize,
            @Parameter(description = "任务名称") @RequestParam(required = false) String taskName,
            @Parameter(description = "运行状态") @RequestParam(required = false) String taskStatus,
            @Parameter(description = "发布状态") @RequestParam(required = false) Integer taskRunStatus,
            @Parameter(description = "负责人") @RequestParam(required = false) String creatorId,
            @Parameter(description = "开始时间")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date startTime,
            @Parameter(description = "结束时间")
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date endTime,
            @Parameter(description = "目录ID") @RequestParam(required = false, defaultValue = "0") Long dirId,
            String order,
            String orderField,
            String cycle) {
        var result = offlineSyncApplicationService.getOfflineSyncTaskPage(
                pageNum, pageSize, taskName, taskStatus, taskRunStatus, creatorId, startTime, endTime, dirId, order, orderField, cycle);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "发布离线同步任务", description = "发布离线同步任务")
    @GetMapping("/publishOfflineSyncTask")
    @LogExecution(operation = EngineOperation.PUBLISH, business = Business.离线同步任务)
    public RestResponseEntity<Boolean> publishOfflineSyncTask(
            @Parameter(description = "离线同步任务id") @RequestParam Long taskId) {
        var result = offlineSyncApplicationService.publishTask(taskId);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "下线离线同步任务", description = "下线离线同步任务")
    @GetMapping("/unpublishOfflineSyncTask")
    @LogExecution(operation = EngineOperation.DEPUBLISH, business = Business.离线同步任务)
    public RestResponseEntity<Boolean> unpublishOfflineSyncTask(
            @Parameter(description = "离线同步任务id", required = true) @RequestParam Long taskId) {
        var result = offlineSyncApplicationService.offlineTask(taskId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTaskRunningHistory")
    @Operation(summary = "获取指定离线同步任务的运行历史记录", description = "获取指定离线同步任务的运行历史记录")
    public RestResponseEntity<PagerResponse<OfflineSyncRunningRecordResponse>> getTaskRunningHistory(
            @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
            @RequestParam(required = false, defaultValue = "1")
            Integer pageNum,
            @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
            @RequestParam(required = false, defaultValue = "10")
            Integer pageSize,
            @Parameter(description = "任务id") @RequestParam(required = true) Long taskId,
            @Parameter(description = "任务状态") @RequestParam(required = false) Integer taskStatus,
            @Parameter(description = "任务类型 0-周期 1-手动") @RequestParam(required = false) Integer executeType) {
        var result = offlineSyncApplicationService.historyTask(pageNum, pageSize, taskId, taskStatus, executeType);
        return RestResponseEntity.success(result);
    }

    // 接口采集任务相关接口
    @PostMapping("/createApiIntegrationTask")
    @Operation(summary = "新建接口采集任务", description = "新建接口采集任务")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.接口采集)
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_API)
    public RestResponseEntity<Boolean> createApiIntegrationTask(@Validated @RequestBody SaveHttpTaskRequest saveTaskRequest) {
        var result = apiHttpApplicationService.saveTask(saveTaskRequest);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/updateApiIntegrationTask")
    @Operation(summary = "保存编辑接口采集任务", description = "保存编辑接口采集任务")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.接口采集)
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_API)
    public RestResponseEntity<Boolean> updateApiIntegrationTask(@Validated @RequestBody SaveHttpTaskRequest saveTaskRequest) {
        var result = apiHttpApplicationService.saveTask(saveTaskRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/deleteApiIntegrationTask")
    @Operation(summary = "删除HTTP接口采集任务", description = "删除HTTP接口采集任务")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.INTEGRATE_TASK_API)
    @LogExecution(operation = EngineOperation.DEL, business = Business.接口采集)
    public RestResponseEntity<Boolean> deleteApiIntegrationTask(
            @Parameter(description = "任务id") @RequestParam(required = true) Long taskId) {
        var result = apiHttpApplicationService.deleteTask(taskId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listApiIntegrationTasks")
    @Operation(summary = "获取HTTP接⼝采集任务列表", description = "获取HTTP接⼝采集任务列表")
    @LogExecution(operation = EngineOperation.SELECT, business = Business.接口采集)
    public RestResponseEntity<PagerResponse<TaskItemDTO>> listApiIntegrationTasks(
            @Parameter(description = "当前页", schema = @Schema(defaultValue = "1"))
            @RequestParam(required = false, defaultValue = "1")
            Integer pageNum,
            @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10"))
            @RequestParam(required = false, defaultValue = "10")
            Integer pageSize,
            @Parameter(description = "任务状态", example = "默认为-1,全部") @RequestParam(required = false)
            Integer taskStatus,
            @Parameter(description = "搜索条件", example = "") @RequestParam(required = false) String search) {
        var result = apiHttpApplicationService.getTaskPage(pageNum, pageSize, taskStatus, search);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getApiIntegrationTask")
    @Operation(summary = "查看HTTP接⼝采集任务详情", description = "查看HTTP接⼝采集任务详情")
    @LogExecution(operation = EngineOperation.SELECT, business = Business.接口采集)
    public RestResponseEntity<HttpTaskDTO> getApiIntegrationTask(
            @Parameter(description = "任务id") @RequestParam(required = true) Long id) {
        var result = apiHttpApplicationService.getTaskById(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/publishApiIntegrationTask")
    @Operation(summary = "发布HTTP接⼝采集任务", description = "发布HTTP接⼝采集任务")
    @LogExecution(operation = EngineOperation.PUBLISH, business = Business.接口采集)
    public RestResponseEntity<Boolean> publishApiIntegrationTask(
            @Parameter(description = "任务id") @RequestParam(required = true) Long taskId) {
        var result = apiHttpApplicationService.publishTask(taskId);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/unpublishApiIntegrationTask")
    @Operation(summary = "下线HTTP接⼝采集同步任务", description = "下线HTTP接⼝采集同步任务")
    @LogExecution(operation = EngineOperation.DEPUBLISH, business = Business.接口采集)
    public RestResponseEntity<Boolean> unpublishApiIntegrationTask(
            @Parameter(description = "任务id") @RequestParam(required = true) Long taskId) {
        var result = apiHttpApplicationService.offlineTask(taskId);
        return RestResponseEntity.success(result);
    }
}